package com.example.homeworkservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.homeworkservice.entity.homework_submit;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface homeworkSubmitMapper extends BaseMapper<homework_submit> {
    
    @Select("SELECT hs.*, h.title as homework_title, h.content as homework_content, " +
            "h.ddl_time, h.total_score, u.username " +
            "FROM homework_submit hs " +
            "LEFT JOIN homework h ON hs.homework_id = h.id " +
            "LEFT JOIN user_account u ON hs.user_id = u.id " +
            "WHERE hs.user_id = #{userId} " +
            "ORDER BY hs.submitted_at DESC")
    List<Map<String, Object>> getStudentSubmissions(@Param("userId") int userId);
    
    @Select("SELECT hs.*, h.title as homework_title, h.content as homework_content, " +
            "h.ddl_time, h.total_score, u.username " +
            "FROM homework_submit hs " +
            "LEFT JOIN homework h ON hs.homework_id = h.id " +
            "LEFT JOIN user_account u ON hs.user_id = u.id " +
            "WHERE hs.homework_id = #{homeworkId} " +
            "ORDER BY hs.submitted_at DESC")
    List<Map<String, Object>> getHomeworkSubmissions(@Param("homeworkId") int homeworkId);
    
    @Select("SELECT h.*, hs.content_text, hs.submitted_at, hs.score, hs.feedback, " +
            "CASE WHEN hs.id IS NOT NULL THEN 1 ELSE 0 END as is_submitted " +
            "FROM homework h " +
            "LEFT JOIN homework_submit hs ON h.id = hs.homework_id AND hs.user_id = #{userId} " +
            "WHERE h.course_id IN ( " +
            "  SELECT course_id FROM enrollment WHERE user_id = #{userId} AND status = 1 " +
            ") " +
            "ORDER BY h.created_at DESC")
    List<Map<String, Object>> getStudentHomeworks(@Param("userId") int userId);
}
